package com.xiaoy;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.support.RequestContextUtils;

public class MyInterceptor extends HandlerInterceptorAdapter{

    public MyInterceptor()
    {
        paramName = "locale";
    }

    public void setParamName(String paramName)
    {
        this.paramName = paramName;
    }

    public String getParamName()
    {
        return paramName;
    }

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws ServletException
    {
        String newLocale = request.getParameter(paramName);
        System.out.println(newLocale);
        if(newLocale != null)
        {
            LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
            if(localeResolver == null)
                throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
            localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
        }
        return true;
    }

    public static final String DEFAULT_PARAM_NAME = "locale";
    private String paramName;

}
